package shuashuami.hb.com.avtivity.agent;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import shuashuami.hb.com.avtivity.AbActivity;
import shuashuami.hb.com.entity.agent.DistributeValue;
import shuashuami.hb.com.fragment.agent.DistributeCompleteFragm;
import shuashuami.hb.com.hbclient.R;
import shuashuami.hb.com.http.HttpAgentMethods;
import shuashuami.hb.com.http.HttpOnNextListener;
import shuashuami.hb.com.view.PagerSlidingTabStrip;

/**
 * created by zhangqi at 2017/7/10
 */
public class DistributionCenterAct extends AbActivity {

    private ViewPager contentPager;
    private mPagerAdapter adapter;
    private PagerSlidingTabStrip tabs;
    private TextView text_value;
    private Fragment fragment;

    private DistributeCompleteFragm mCompleteFragm,mNotCompleteFragm;
    @Override
    protected void initUI() {
        setContentView(R.layout.act_distributecenter);
        setTitleBar("分销中心");
        setLeftView();

        text_value= (TextView) findViewById(R.id.text_value);
        contentPager = (ViewPager) findViewById(R.id.content_pager_aclick);
        adapter = new mPagerAdapter(getSupportFragmentManager());
        contentPager.setAdapter(adapter);
        contentPager.setOffscreenPageLimit(2);
//        contentPager.setPageTransformer(true, new ZoomOutPageTransformer());

        tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs_aclick_center);
        tabs.setTextColorResource(R.color.gray);
        tabs.setDividerColorResource(R.color.view_unpressed);
        tabs.setIndicatorColorResource(R.color.blue);
        tabs.setSelectedTextColorResource(R.color.blue);
        tabs.setIndicatorHeight(4);
        tabs.setUnderlineHeight(2);
        tabs.setTextSize(12);
        tabs.setViewPager(contentPager);
    }

    @Override
    protected void initData() {
//        mCompleteFragm = new DistributeCompleteFragm(1);
//        mNotCompleteFragm=new DistributeCompleteFragm(0);

        getDataFromSever();

    }

    public void getDataFromSever(){
        HttpOnNextListener<DistributeValue> listener=new HttpOnNextListener<DistributeValue>() {
            @Override
            public void onNext(DistributeValue o) {

                Message msg=Message.obtain();
                msg.what=0x1;
                msg.obj=o.getMoney();
                mHandler.sendMessage(msg);

            }
        };

        HttpAgentMethods.getDistributeBaseData(listener);
    }

    @Override
    protected void initListener() {
        setLeftViewListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DistributionCenterAct.this.finish();
            }
        });
    }

    private class mPagerAdapter extends FragmentStatePagerAdapter {

        private String Title[] = {"已完成","未完成"};

        public mPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int arg0) {
            // int[] id = { 5, 18, 27, 37, 21, 36, 23, 24 };
            if (arg0 == 0) {
                if(mCompleteFragm==null){
                    Bundle bundle=new Bundle();
                    bundle.putInt("type",1);
                    mCompleteFragm=new DistributeCompleteFragm();
                    mCompleteFragm.setArguments(bundle);
                }
                return mCompleteFragm;
            } else {
                if(mNotCompleteFragm==null){
                    Bundle bundle=new Bundle();
                    bundle.putInt("type",0);
                    mNotCompleteFragm=new DistributeCompleteFragm();
                    mNotCompleteFragm.setArguments(bundle);
                }

                return mNotCompleteFragm;
            }
        }

        @Override
        public int getCount() {
            return Title.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return Title[position];
        }

    }

    Handler mHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            int what=msg.what;
            if(what==0x1){
                String value=msg.obj.toString();
                text_value.setText(value);
            }
        }
    };
}
